<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;
use Laravel\Passport\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use App\Notifications\ResetPasswordNotification;

class Character extends Authenticatable
{

    use HasApiTokens,Notifiable;

    protected $table = 'characters';

    public    $timestamps = false;
    protected $primaryKey = 'id';
    /**
     * 返回列表数据
     * @param $id
     * @return mixed
     */
    public function getList($where,$data,$orderBy1='id' , $orderBy2='desc')
    {
        $username=$data['username'];
        $list=self::where($where)
            ->when($username,function ($query,$username){
                $query->where('username', 'LIKE', "%{$username}%");
            })
            ->orderBy($orderBy1,$orderBy2)
            ->forPage($data['page'], $data['pageSize'])
            ->get()
            ->toArray();

        $count=self::where($where)
            ->when($username,function ($query,$username){
                $query->where('username', 'LIKE', "%{$username}%");
            })->count();

        $result = [
            'list'   =>  $list,
            'total' =>  $count
        ];
        return $result;
    }

    public function info(array $data):array {
        return self::where(['id'=>$data['id']])
            ->select('username','img','face_id','hair_id','expression_id','cloth_id','face_img','after_hair_img','front_hair_img','expression_img','cloth_img')
            ->first()->toArray();
    }

    public function edit(array $data):bool{
        $up['img']=$data['img'];
        $up['face_id']=$data['face_id'];
        $up['hair_id']=$data['hair_id'];
        $up['expression_id']=$data['expression_id'];
        $up['cloth_id']=$data['cloth_id'];
        $up['face_img']=str_replace(env('OSS_URL',''),'',$data['face_img']);
        $up['front_hair_img']=str_replace(env('OSS_URL',''),'',$data['front_hair_img']);
        $up['after_hair_img']=str_replace(env('OSS_URL',''),'',$data['after_hair_img']);
        $up['expression_img']=str_replace(env('OSS_URL',''),'',$data['expression_img']);
        $up['cloth_img']=str_replace(env('OSS_URL',''),'',$data['cloth_img']);

        return self::where(['id'=>$data['id']])->update($up);
    }


}
